package com.ibeeking.found.upms.b.rest.controller.v1;

import com.ibeeking.found.upms.b.rest.service.IOpenUrlRelationService;
import com.ibeeking.found.upms.b.rest.service.IOpenUrlService;
import com.ibeeking.found.upms.service.common.bo.OpenUrlRelationBO;
import com.ibeeking.found.upms.service.common.param.OpenUrlParam;
import com.ibeeking.found.upms.service.common.param.OpenUrlUserParam;
import com.ibeeking.found.upms.service.common.query.OpenUrlPageQuery;
import com.ibeeking.found.upms.service.common.vo.OpenUrlPageVO;
import com.ibeeking.found.upms.service.common.vo.OpenUrlVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName OpenUrlController控制器
 * @Description open
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/b/openurl/v1")
public class OpenUrlController {

    @Resource
    private IOpenUrlService openUrlServiceImpl;
    @Resource
    private IOpenUrlRelationService openUrlRelationServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询openUrl列表")
    @PostMapping(value = "/pageList")
    public Page<OpenUrlPageVO> pageList(@RequestBody OpenUrlPageQuery query) {
        return openUrlServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增开放URL")
    @PostMapping(value = "/add")
    public Boolean add(@RequestBody @Validated(BaseValidationGroup.Insert.class) OpenUrlParam param) {
        return openUrlServiceImpl.add(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改开放URL")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) OpenUrlParam param) {
        return openUrlServiceImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询开放URL")
    @GetMapping(value = "/view/{id}")
    public OpenUrlVO view(@PathVariable("id") Long id) {
        return openUrlServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "启用/禁用开放URL")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return openUrlServiceImpl.changeStatus(id, status);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除开放URL")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return openUrlServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "开放路径关联open用户")
    @PostMapping(value = "/modifyByOpenUrlId")
    public Boolean modifyByOpenUrlId(@RequestBody @Validated OpenUrlUserParam param) {
        return openUrlRelationServiceImpl.modifyByOpenUrlId(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询选中OpenUserId列表")
    @GetMapping(value = "/queryOpenUserIdsByUrlId/{openUrlId}")
    public List<String> queryOpenUserIdsByUrlId(@PathVariable("openUrlId") @NotNull(message = "openUrlId不能为空") Long openUrlId) {
        List<OpenUrlRelationBO> openUrlRelationBos = openUrlRelationServiceImpl.queryByUrlId(openUrlId);
        return openUrlRelationBos.parallelStream().map(openUrlRelationBO -> String.valueOf(openUrlRelationBO.getOpenId())).distinct().collect(Collectors.toList());
    }
}
